demos.ROSpanel = Ext.extend( Ext.Panel, {
    btnCount: 0,
    bitWise: 0,
    height: '46',
    ROStitle: [],
    ROSitems: [],
	panelItems: [],
    layoutOnOrientationChange: true,
	defaults: {
	},
//   html: 'Test1',
//   id: ROSpanel,
    initComponent: function() {
//    	demos.ROSpanel2.panelItems = new Array();
    	
//    	var Title = this.items.items[0].text;
    	
    	var ROStitle = this.ROStitle[0];
    	
    	this.addEvents('rosbutton');
        this.enableBubble('rosbutton');

        Ext.apply(this, {
        	dock: 'top',
        	scroll: 'vertical',
	    	dockedItems: [
	    	    {
        	    	xtype:	'panel',
        	    	width: Ext.is.Phone ? 90 : 164,
        	    	dock:	'left',
	      	        layoutOnOrientationChange: true,
        	    	dockedItems: [
		                {
			      	    	xtype:	'toolbar',
			      	    	id:		'ros_'+ROStitle.id+'_toolbartitle',
			      	    	dock:	'top',
		      	            ui: 	this.ui,
			      	        layoutOnOrientationChange: true,
			      	        defaults: {
			      	            xtype: 'button',
			               	    ui: 'action',
			               	    cls: 'flat-x-button',
			      	            width: Ext.is.Phone ? 80 : 150,
			      	        },
			      	        items:	[{
			      	        	id: 'ros_'+ROStitle.id+'_titlebutton',
			      	        	text: ROStitle.text
			      	        }]
		                }
	                ]
	      	    }, {
	      	    	xtype:	'toolbar',
	      	    	id:		'ros_'+ROStitle.id+'_toolbarmain',
	      	    	dock:	'top',
	      	        ui: 	this.ui,
	      	        layoutOnOrientationChange: true,
	          		scroll: {
	      	            direction: 'horizontal',
	      	            useIndicators: false
	      	        },
	      	        defaults: {
	      	            xtype: 'button',
	      	            width: 120,
	      	            scope: this,
	      	            handler:  this.buttonPN,
	      	        },
	            	items: this.ROSitems,
	    	        panelItems: this
	      	    }
	      	]
        });
        
        demos.ROSpanel.superclass.initComponent.apply(this, arguments);
    },
    
    
//	getBubbleTarget: function() {
//	    return examcontrol;
//	},
	
    buttonPN: function (btn, evt) {
        //Setting up the button ui...    	
    	var panels = this;
    	
    	//console.debug(this.id);
    	
    	//ros_constitution.dockedItems.items[0].dockedItems.items[0].items.items[0].badgeText

    	
        var bitwise = 0;
        var temp = panels.btnCount;
        var ROSbadge = panels.dockedItems.items[0].dockedItems.items[0].items;
        var ROSitems = panels.dockedItems.items[1].items;
        var ROSbNumber = parseInt(panels.btnCount);
            ROSnNumber = isNaN(ROSbNumber) ? 0 : ROSbNumber;

        //console.debug(ROSbadge.items[0].id+'::'+ROSbadge.items[0].badgeText);
            
            
        var chgBadge = function(x, idx) {
//        	if (idx>1) {
                bitwise += temp%2;
                temp = Math.floor(temp/2);
//            }
        };

        var isThisAdd = function(x, idx) {
            // returns true if x is current item
//        	console.debug('isThisAdd x='+x+',idx='+idx+'btn.id='+btn.id+',ROSi.id='+ROSitems.items[idx].id);
            if (btn.id == ROSitems.items[idx].id) {
                ROSnNumber += Math.pow(2,idx);
                panels.btnCount = ROSnNumber;
//                        console.debug(ROSpanel.btnCount+':'+temp+':'+bitwise);
//                        ToolbarDemo.views.Patientdetail.update;
            }
        };

        var isThisSub = function(x, idx) {
            // returns true if x is current item
//        	console.debug('isThisSub x='+x+',idx='+idx+'btn.id='+btn.id+',ROSi.id='+ROSitems.items[idx].id);
            if (btn.id == ROSitems.items[idx].id) {
                ROSnNumber -= Math.pow(2,idx);
//            	console.debug('ROSnNumber='+ROSnNumber);
                temp -= Math.pow(2,idx);
                panels.btnCount = ROSnNumber;
//                        console.debug(ROSpanel.btnCount+':'+temp+':'+bitwise);
//                ROSbadge.items[0].update;
//                        ToolbarDemo.views.Patientdetail.update;
            }
        };

        if(btn.ui == 'normal'){
            btn.ui = 'confirm';
            btn.el.removeCls('x-button-normal');
            btn.el.addCls('x-button-confirm');
            ROSitems.items.forEach(isThisAdd);
            temp = panels.btnCount;
            ROSitems.items.forEach(chgBadge);
            ROSbadge.items[0].setBadge(bitwise);
//                    Ext.getCmp('const1').setBadge(bitwise);
//                    Ext.getCmp('const2').setBadge(bitwise);
//                    console.debug(bitwise);
//                    console.debug(this.id);
//                    ROSbadge.Button.setBadge(this.getId());
//                    console.log(ToolbarDemo.ROSpanel.items.items.indexOf(this.id));  //.tab.badgeText = '1';
        } else {
            if (btn.ui == 'confirm'){
            btn.ui = 'decline';                                
            btn.el.removeCls('x-button-confirm');
            btn.el.addCls('x-button-decline');
            bitwise = panels.bitWise;
            } else {
                btn.ui = 'normal';
                btn.el.removeCls('x-button-decline');
                btn.el.addCls('x-button-normal');
                ROSitems.items.forEach(isThisSub);
                temp = panels.btnCount;
                ROSitems.items.forEach(chgBadge);
      // KR          console.debug(bitwise);
                ROSbadge.items[0].setBadge(bitwise);
      //          Ext.getCmp('const1').setBadge(bitwise);
      //          Ext.getCmp('const2').setBadge(bitwise);
            }
        };
        
//        ROSbadge.items[0].doComponentLayout();
        
        panels.bitWise = bitwise;

        this.doComponentLayout();
        this.fireEvent('rosbutton');
    }
});

Ext.reg('rospanel', demos.ROSpanel);
